﻿using Furion;
using IMES.BLL.Dtos;
using IMES.BLL;
using IMES.Utils;

namespace IMES
{
    public partial class Login : AntdUI.Window
    {
        public Login()
        {
            InitializeComponent();
            InitData();
        }

        private void InitData()
        {
            //预置常见规则 

            string cachePwd = ConfigHelper.Instance.GetInitConfig("AppSettings", "CachePassword");
            string userName = ConfigHelper.Instance.GetInitConfig("AuthInfo", "UserName");
            txtUserName.Text = userName;

            if (!string.IsNullOrWhiteSpace(cachePwd) && cachePwd.ToLower().Equals("true"))
            {
                checkCachePwd.Checked = true;
                string userPwd = ConfigHelper.Instance.GetInitConfig("AuthInfo", "Password");
                userPwd = DESEncryptHelper.Instance.DESDecrypt(userPwd);
                txtPwd.Text = userPwd;
            }
        }
        private bool Validation()
        {
            bool isNext = true;
            if (string.IsNullOrWhiteSpace(txtUserName.Text))
            {
                TipsShow(true, "账号不能为空!");
                return false;
            }
            if (string.IsNullOrWhiteSpace(txtPwd.Text))
            {
                TipsShow(true, "密码不能为空!");
                return false;
            }

            return isNext;
        }


        private void btnLogin_Click(object sender, EventArgs e)
        {
            bool isNext = Validation();
            if (!isNext) return;
            btnLogin.Loading = true;
            AntdUI.ITask.Run(() =>
            {
                UserRep urep = App.GetService<UserRep>();
                string userName = txtUserName.Text;
                string userPwd = txtPwd.Text;
                UserVO user = urep.LoginCheck(userName, userPwd);

                if (user != null && user.Id > 0)
                {
                    //登录账号缓存
                    LocalComputerHelper.Instance.CacheLoginAccount(user);
                    //默认选项处理
                    saveDefaultCheckManager(userName, userPwd);

                    if (btnLogin.IsDisposed) return;
                    btnLogin.Loading = false;
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    this.DialogResult = DialogResult.None;
                    TipsShow(true, "账号或密码错误!");
                    btnLogin.Loading = false;
                }
            });
        }

        /// <summary>
        /// 显示/隐藏登录提示
        /// </summary>
        /// <param name="isShow"></param>
        /// <param name="tipTxt"></param>
        private void TipsShow(bool isShow, string tipTxt)
        {
            if (!isShow) { alertMsg.Visible = false; return; }
            alertMsg.Icon = AntdUI.TType.Error;
            alertMsg.Text = tipTxt;
            alertMsg.Visible = true;
        }

        /// <summary>
        /// 保存默认勾选项处理
        /// </summary>
        private void saveDefaultCheckManager(string userName, string userPwd)
        {
            bool savePassword = checkCachePwd.Checked;
            ConfigHelper.Instance.SaveInitConfig("AppSettings", "CachePassword", savePassword.ToString().ToLower());
            ConfigHelper.Instance.SaveInitConfig("AuthInfo", "UserName", userName);
            if (savePassword)
            {
                ConfigHelper.Instance.SaveInitConfig("AuthInfo", "Password", DESEncryptHelper.Instance.DESEncrypt(userPwd), true);
            }
            else
            {
                ConfigHelper.Instance.SaveInitConfig("AuthInfo", "Password", "", true);
            }
        }

        private void txtPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)//如果输入的是回车键  
            {
                this.btnLogin_Click(sender, e);//触发button事件  
            }
        }
    }
}
